package org.mtdownload.chartview;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.Scrollable;

import org.mtdownload.download.DownloadTask;
import org.mtdownload.utils.CircularEnumeration;
import org.mtdownload.utils.PreferencesManager;

public class ChartPanel extends JPanel implements Scrollable {

	private static final long serialVersionUID = 3256721779816216370L;
	private List<Color> colorsList=null;
	private CircularEnumeration<Color> colorsEnumeration=new CircularEnumeration<Color>(); 
	private TaskSeries series;
	private static final int THREAD_CHART_HEIGHT=100;
	
	public ChartPanel() {
		/*empty constructor*/
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		if(series==null)return;
		if(series.getMaxSpeed()==0)return;
		colorsList=PreferencesManager.getProgressColorsList();
		colorsEnumeration.setList(colorsList);	
		Graphics2D g2=(Graphics2D)g;
		Dimension size=this.getSize();
		int taskHeight=THREAD_CHART_HEIGHT;
		//if(size.height>taskHeight*series.getThreadsNumber()){
			//taskHeight=size.height/series.getThreadsNumber();
		//}
		for(int i=0;i<series.getThreadsNumber();i++){
			g2.setPaint(Color.BLACK);
			g2.drawLine(0,i*taskHeight,size.width,i*taskHeight);
		}
		
		for(int i=size.width,speedIndex=series.getSpeedsList().size()-1;i>=0 && speedIndex>=0 ;i--,speedIndex--){					
			long[] speeds=series.getSpeedsList().get(speedIndex);
			colorsEnumeration.resetIndex();
			for(int j=0;j<speeds.length;j++){
				int h=0;	
				Color paint=colorsEnumeration.nextElement();
				g2.setPaint(paint);
				if(series.getMaxThreadSpeed(j)==0)continue;
				h=(int)(speeds[j]*taskHeight/series.getMaxThreadSpeed(j));
				g2.drawLine(i,taskHeight*(j+1),i,(taskHeight*(j+1))-h);

				if(i==size.width){
					String speedString="Thread"+j+": "+DownloadTask.getHumanFriendlySpeed(DownloadTask.SPEED_KILOBYTES,speeds[j]) + " KB/s";
					g2.setPaint(Color.WHITE);
					Rectangle2D stringBounds=g2.getFontMetrics().getStringBounds(speedString,g2);
					g2.fillRect(2,taskHeight*j+2,(int)stringBounds.getWidth()+2,(int)stringBounds.getHeight()+2);
					g2.setPaint(paint);
					g2.drawString(speedString,2,(int)stringBounds.getHeight()+taskHeight*j+2);
				}
				
			}			
		}
	}
	
	public Dimension getPreferredSize(){
		if(series==null){
			return new Dimension(100,THREAD_CHART_HEIGHT);	
		}else{			
			return new Dimension(100,series.getThreadsNumber()*THREAD_CHART_HEIGHT);
		}				
	}

	public Dimension getPreferredScrollableViewportSize() {
		return getPreferredSize();
	}

	public int getScrollableUnitIncrement(Rectangle visibleRect,
			int orientation, int direction) {
		return THREAD_CHART_HEIGHT;
	}

	public int getScrollableBlockIncrement(Rectangle visibleRect,
			int orientation, int direction) {
		return THREAD_CHART_HEIGHT*2;
	}

	public boolean getScrollableTracksViewportWidth() {
		return true;
	}

	public boolean getScrollableTracksViewportHeight() {
		return false;
	}

	/**
	 * @return Returns the series.
	 */
	public TaskSeries getSeries() {
		return series;
	}
	

	/**
	 * @param series The series to set.
	 */
	public void setSeries(TaskSeries series) {
		this.series = series;
	}
	

}
